package org.example.everyday;

import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;

import java.util.ArrayList;
import java.util.List;

public class Lc2049 {

    public static void main(String[] args) {
        Lc2049 lc2049 = new Lc2049();
        int[] parents = {-1,2,0,2,0};
        int result = lc2049.countHighestScoreNodes(parents);
        System.out.println(result);
    }


        long maxScore = 0;
        int cnt = 0;
        int n;
        List<Integer>[] children;

        public int countHighestScoreNodes(int[] parents) {
          n = parents.length;
          children = new List[n];
          for (int i = 0; i < n; i++) {
               children[i] = new ArrayList<Integer>();
          }
          for (int i = 0; i < n; i++) {
              int p  = parents[i];
              if(p!=-1){
                  children[p].add(i);
              }
          }


          dfs(0);
          return cnt;
        }
        private int dfs(int node) {
          int size = n - 1;
          long score = 1;
          for (int i : children[node]){
              int t = dfs(i);
              size -= t;
              score *= t;
          }
          System.out.println("node="+node+";size="+size);
          if(node != 0){
              score *= size;
          }
          if(score == maxScore){
              cnt++;
          }else if(score > maxScore){
              maxScore = score;
              cnt = 1;
          }
          return n - size;
        }


}
